//
//  PGCalendarItem.m
//  TestCalendar
//
//  Created by Pill Gong on 14-2-17.
//  Copyright (c) 2014年 Pill Gong. All rights reserved.
//

#import "PGCalendarItem.h"
#import "UIView+Addtions.h"
#import "EpointDateUtils.h"
#import "LunarCalendar.h"

@interface PGCalendarItem ()

@property (nonatomic, strong) UILabel *eventLaebl;

@end

@implementation PGCalendarItem

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.dateLabel.backgroundColor=[UIColor clearColor];
        [self addSubview:_dateLabel];
        
        self.chinaDate = [[UILabel alloc] initWithFrame:CGRectZero];
        self.chinaDate.backgroundColor=[UIColor clearColor];
        [self addSubview:_chinaDate];
        
        self.todayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"today_bg"]];
        [self addSubview:_todayImage];
        _todayImage.hidden = YES;
        
        self.eventLaebl = [[UILabel alloc] initWithFrame:CGRectZero];
//        [self addSubview:_eventLaebl];
        _eventLaebl.hidden = YES;

        [self setDefaultValues];
        
        _stateColor = [UIColor clearColor];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemTaped:)];
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

- (void)setDefaultValues {
    
    if (CurrentDeviceISiPad) {
        self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        self.layer.borderWidth = 0.5;
    }
    if (CurrentDeviceISiPad) {
        _dateLabel.font = [UIFont boldSystemFontOfSize:22];
        _chinaDate.font = [UIFont systemFontOfSize:18];
        _eventLaebl.font = [UIFont systemFontOfSize:16];
    } else {
        _dateLabel.font = [UIFont boldSystemFontOfSize:16];
        _chinaDate.font = [UIFont systemFontOfSize:12];
        _eventLaebl.font = [UIFont systemFontOfSize:10];
    }
    
    _eventLaebl.layer.cornerRadius = 3;
    _eventLaebl.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _eventLaebl.layer.borderWidth = 1;
    _eventLaebl.textAlignment = NSTextAlignmentCenter;
    _eventLaebl.backgroundColor = MainTinColor;
    _eventLaebl.textColor = [UIColor whiteColor];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [_dateLabel sizeToFit];
    _dateLabel.center = CGPointMake(self.width*0.5, self.height*0.5);
    
    [_chinaDate sizeToFit];
    _chinaDate.centerX = _dateLabel.centerX;
    _chinaDate.top = _dateLabel.bottom;
    
    if (_isToday) {
        _todayImage.frame = CGRectMake(0, 0, self.width, self.height);
        _todayImage.contentMode = UIViewContentModeScaleAspectFit;
        _todayImage.hidden = NO;
        _todayImage.center = _dateLabel.center;
    }

    if (_eventLaebl.hidden == NO) {
        [_eventLaebl sizeToFit];
        if (CurrentDeviceISiPad) {
            _eventLaebl.width = (_eventLaebl.text.length < 2?24:_eventLaebl.width+5);
        } else {
            _eventLaebl.width = (_eventLaebl.text.length < 2?12:_eventLaebl.width+2);
        }
        _eventLaebl.height += 2;
        _eventLaebl.bottom = _dateLabel.top;
        _eventLaebl.right = self.width-3;
    }
}

- (void)setItemDate:(NSDate *)itemDate {
    
    if (itemDate == nil) {
        return;
    }
    _itemDate = itemDate;
    _dateLabel.text = [EpointDateUtils getDay:_itemDate];
    
    LunarCalendar *lunarCalendar = [_itemDate chineseCalendarDate];
    if (lunarCalendar.SolarTermTitle.length <= 0) {
        if ([lunarCalendar.DayLunar isEqualToString:@"初一"]) {
            _chinaDate.text = lunarCalendar.MonthLunar;
        } else {
            _chinaDate.text = lunarCalendar.DayLunar;
        }
    } else {
        _chinaDate.text = lunarCalendar.SolarTermTitle;
    }
}

- (void)setEventCount:(int)eventCount {
    _eventCount = eventCount;
    _eventLaebl.text = [NSString stringWithFormat:@"%d", _eventCount];
    if (_eventCount != 0) {
        NSLog(@"show");
//        _eventLaebl.hidden = NO;
        self.backgroundColor = COLOR(221, 160, 221);
        self.stateColor =COLOR(221, 160, 221);
        [self setNeedsLayout];
    }
}

- (void)itemTaped:(UITapGestureRecognizer *)tap {
    if (tap.state == UIGestureRecognizerStateRecognized) {
        self.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0 alpha:0.8];
        if (_delegate && [_delegate respondsToSelector:@selector(itemPressed:)]) {
            [_delegate itemPressed:self];
        }
    }
}


@end
